#include "reservation.h"
#include "ui_reservation.h"
#include "qabstractitemview.h"
#include "lib/sql.h"
#include "ty_res.h"

reservation::reservation(QWidget *parent,int f)
    : QWidget(parent)
    , ui(new Ui::reservation)
{
    fla = f;
    ui->setupUi(this);
    ui->tableView->setModel(&r_res);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

reservation::~reservation()
{
    delete ui;
}

void reservation::initpage(QString strCondition)
{
    if(fla == 1){
        ui->alter->hide();
        ui->return_2->hide();
    }
    auto l=Sql::getinstance()->getret(strCondition);
    r_res.clear();
    r_res.setHorizontalHeaderLabels(QStringList{"用户编号","门牌号","联系电话","预约日期","预约时间","结束时间"});
    for(int i=0;i<l.size();i++){
        QList<QStandardItem*> items;
        for(int j=0;j<l[i].size();j++)
        {
            items.append(new QStandardItem(l[i][j]));
        }
        r_res.appendRow(items);
    }
}


void reservation::on_lineEdit_textChanged(const QString &arg1)
{
    //搜索栏
    QString strSql = QString("where door like '%%1%' or userid like '%%1%'").arg(arg1);
    initpage(strSql);
}


void reservation::on_return_2_clicked()
{
    //撤回预约记录
    int r = ui->tableView->currentIndex().row();
    if(r < 0){
        QMessageBox::information(nullptr,"信息","无选中记录");
    }
    else{
        auto id = r_res.item(r,1)->text();
        Sql::getinstance()->delres(id);
        initpage();
    }
}


void reservation::on_alter_clicked()
{
    //修改预约记录
    int r = ui->tableView->currentIndex().row();
    if(r < 0){
        QMessageBox::information(nullptr,"信息","无选中记录");
    }
    else{
        auto id = r_res.item(r,0)->text();
        ty_res ty;
        ty.setType(id.toInt());
        ty.exec();
        initpage();
    }
}

